package lifeOfAFish.plantModels.corals.sebea;

import lifeOfAFish.Fish;
import lifeOfAFish.scenegraph.sgNode;
import lifeOfAFish.scenegraph.sgTransform;
import lifeOfAFish.scenegraph.geom.sgGeomModel;

/**
 * @author Christian Mletzko
 *
 */
public class Sebea extends Fish {

	
	private int frameCounter = Integer.MIN_VALUE;
	private int finCounter = -10;
	private int slowFinCounter = -2;
	private boolean finMovementUp;
	private boolean slowFinMovementUp;
	private float theta;
	
	private SebeaPreferences a_prefs;
	private sgNode head;
	private sgGeomModel sebea_0;	
	private sgGeomModel sebea_1;
	private sgGeomModel sebea_2;
	private sgGeomModel sebea_3;
	private sgGeomModel sebea_4;
	private sgGeomModel sebea_5;
	private sgGeomModel sebea_6;
	private sgGeomModel sebea_7;
	
	private sgTransform sebea_0_Rotation;
	private sgTransform sebea_1_Rotation;
	private sgTransform sebea_2_Rotation;
	private sgTransform sebea_3_Rotation;
	private sgTransform sebea_4_Rotation;
	private sgTransform sebea_5_Rotation;
	private sgTransform sebea_6_Rotation;
	private sgTransform sebea_7_Rotation;
	
	
	
	public Sebea() {
		a_prefs = new SebeaPreferences();
		head = new sgNode();
		sebea_0 = new sgGeomModel();
		sebea_1 = new sgGeomModel();
		sebea_2 = new sgGeomModel();
		sebea_3 = new sgGeomModel();
		sebea_3 = new sgGeomModel();
		sebea_4 = new sgGeomModel();
		sebea_5 = new sgGeomModel();
		sebea_6 = new sgGeomModel();
		sebea_7 = new sgGeomModel();
				
	}
	
	
	public void init() {
	
		// Load meshes
		sebea_0.setOBJFileName(a_prefs.getSEBEA_0());
		sebea_0.load();
		
		
		sebea_1.setOBJFileName(a_prefs.getSEBEA_1());
		sebea_1.load();
		
		
		sebea_2.setOBJFileName(a_prefs.getSEBEA_2());
		sebea_2.load();
		
		sebea_3.setOBJFileName(a_prefs.getSEBEA_3());
		sebea_3.load();
		
		sebea_4.setOBJFileName(a_prefs.getSEBEA_4());
		sebea_4.load();
		
		sebea_5.setOBJFileName(a_prefs.getSEBEA_5());
		sebea_5.load();
		
		sebea_6.setOBJFileName(a_prefs.getSEBEA_6());
		sebea_6.load();
		
		sebea_7.setOBJFileName(a_prefs.getSEBEA_7());
		sebea_7.load();
				
		
		/* Add transformation nodes for fins
		 * 1. Rotation
		 * 2. Translation
		 */
		
		// Rotate body
//		sebea_0_Rotation = new sgTransform();
//		sebea_0_Rotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
//		// Translate body
//		sgTransform sebea_0_Translation = new sgTransform();
//		sebea_0_Translation.translate(0.0f, 0.0f, 0.0f);
//		sebea_0_Rotation.addChild(sebea_0_Translation);
		
		
//		sgTransform scaleB1 = new sgTransform();
////		scaleB1.scale(100.0f, 100.0f, 100.0f);
//		sebea_0_Translation.addChild(scaleB1);
//		scaleB1.addChild(sebea_0);
//		
//		head.addChild(sebea_0_Rotation);
		
		
		
		// Rotate fin_bottom_middle_left
		sebea_1_Rotation = new sgTransform();
		sebea_1_Rotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_bottom_middle_left
		sgTransform sebea_1_Translation = new sgTransform();
		sebea_1_Translation.translate(0.0f, 0.0f, 0.0f);
		sebea_1_Rotation.addChild(sebea_1_Translation);
				
		sgTransform scaleB2 = new sgTransform();
//		scaleB2.scale(100.0f, 100.0f, 100.0f);
		sebea_1_Translation.addChild(scaleB2);
		scaleB2.addChild(sebea_1);
		
		head.addChild(sebea_1_Rotation);
		
		
		
		// Rotate fin_bottom_middle_right
		sebea_2_Rotation = new sgTransform();
		sebea_2_Rotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate body
		sgTransform sebea_2_Translation = new sgTransform();
		sebea_2_Translation.translate(0.0f, 0.0f, 0.0f);
		sebea_2_Rotation.addChild(sebea_2_Translation);
				
		sgTransform scaleB3 = new sgTransform();
//		scaleB3.scale(100.0f, 100.0f, 100.0f);
		sebea_2_Translation.addChild(scaleB3);
		scaleB3.addChild(sebea_2);
		
		head.addChild(sebea_2_Rotation);
		
		
		
		// Rotate fin_bottom
		sebea_3_Rotation = new sgTransform();
		sebea_3_Rotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_bottom
		sgTransform sebea_3_Translation = new sgTransform();
		sebea_3_Translation.translate(0.0f, 0.0f, 0.0f);
		sebea_3_Rotation.addChild(sebea_3_Translation);
		
		sgTransform scaleB4 = new sgTransform();
//		scaleB4.scale(100.0f, 100.0f, 100.0f);
		sebea_3_Translation.addChild(scaleB4);
		scaleB4.addChild(sebea_3);
		
		head.addChild(sebea_3_Rotation);
		
		
		// Rotate fin_front_middle_left
		sebea_4_Rotation = new sgTransform();
		sebea_4_Rotation.rotate(theta, 1.0f, 0.0f, 0.0f);
		// Translate fin_front_middle_left
		sgTransform sebea_4_Translation = new sgTransform();
		sebea_4_Translation.translate(0.0f, 0.0f, 0.0f);
		sebea_4_Rotation.addChild(sebea_4_Translation);
		
		sgTransform scaleB5 = new sgTransform();
//		scaleB5.scale(100.0f, 100.0f, 100.0f);
		sebea_4_Translation.addChild(scaleB5);
		scaleB5.addChild(sebea_4);
		
		head.addChild(sebea_4_Rotation);
		
		
		// Rotate fin_front_middle_right
		sebea_5_Rotation = new sgTransform();
		sebea_5_Rotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_front_middle_right
		sgTransform sebea_5_Translation = new sgTransform();
		sebea_5_Translation.translate(0.0f, 0.0f, 0.0f);
		sebea_5_Rotation.addChild(sebea_5_Translation);
		
		sgTransform scaleB6 = new sgTransform();
//		scaleB6.scale(100.0f, 100.0f, 100.0f);
		sebea_5_Translation.addChild(scaleB6);
		scaleB6.addChild(sebea_5);
		
		head.addChild(sebea_5_Rotation);
		
		
		// Rotate fin_main
		sebea_6_Rotation = new sgTransform();
		sebea_6_Rotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_main
		sgTransform sebea_6_Translation = new sgTransform();
		sebea_6_Translation.translate(0.0f, 0.0f, 0.0f);
		sebea_6_Rotation.addChild(sebea_6_Translation);
		
		sgTransform scaleB7 = new sgTransform();
//		scaleB7.scale(100.0f, 100.0f, 100.0f);
		sebea_6_Translation.addChild(scaleB7);
		scaleB7.addChild(sebea_6);
		
		head.addChild(sebea_6_Rotation);
		
		
		// Rotate fin_top_front
		sebea_7_Rotation = new sgTransform();
		sebea_7_Rotation.rotate(0.0f, 1.0f, 0.0f, 0.0f);
		// Translate fin_top_front
		sgTransform sebea_7_Translation = new sgTransform();
		sebea_7_Translation.translate(0.0f, 0.0f, 0.0f);
		sebea_7_Rotation.addChild(sebea_7_Translation);
		
		sgTransform scaleB8 = new sgTransform();
//		scaleB8.scale(100.0f, 100.0f, 100.0f);
		sebea_7_Translation.addChild(scaleB8);
		scaleB8.addChild(sebea_7);
		
		head.addChild(sebea_7_Rotation);
		
			
		// Add head node to local root
		sgTransform magTransform = new sgTransform();
		magTransform.scale(0.40f, 0.4f, 0.4f);
		m_node.addChild(magTransform);
		magTransform.addChild(head);
		
		finMovementUp = true;
		slowFinMovementUp = true;
		theta = -10.0f;
	}
	
	
	
	/**
	 * 
	 */
	public void update() {
		
		incFrameCounter();
		
		// Movement of the fins
		if (frameCounter%3==0){
			
			if (finMovementUp){
				incFinCounter();
			}
			else if(!finMovementUp){
				decFinCounter();
			}
		}
		
		// Movement of the slow fins
		if (frameCounter%8==0){
			
			if (slowFinMovementUp){
				incSlowFinCounter();
			}
			else if(!slowFinMovementUp){
				decSlowFinCounter();
			}
		}
		
		
		sebea_1_Rotation.rotate(theta+0.3f, 1.0f, 0.0f, 0.0f);
		sebea_2_Rotation.rotate(-theta-0.1f, 1.0f, 0.0f, 0.0f);
		sebea_3_Rotation.rotate(theta+0.1f, 0.0f, 1.0f, 0.0f);
		sebea_4_Rotation.rotate(theta-0.2f, 1.0f, 0.0f, 0.0f);
		sebea_5_Rotation.rotate(-theta, 1.0f, 0.0f, 0.0f);
		sebea_6_Rotation.rotate(theta+0.4f, 0.0f, 1.0f, 0.0f);
		sebea_7_Rotation.rotate(theta-0.4f, 1.0f, 0.0f, 0.0f);
		

	}
	
	
	/**
	 * This method increases the frame counter
	 * 
	 * @see getFrameCounter()
	 */
	private void incFrameCounter(){
		if (frameCounter < Integer.MAX_VALUE){
			frameCounter++;
		}
		else
			frameCounter = Integer.MIN_VALUE;
	}
	
	/**
	 * This method increases the fin counter
	 * 
	 * @see getFrameCounter()
	 */
	private void incFinCounter(){
		
			finCounter++;
			theta = theta+0.1f;
			if (finCounter==10){
				finMovementUp = false;
			}
	}
		
	/**
	 * This method increases the fin counter
	 * 
	 * @see getFrameCounter()
	 */
	private void decFinCounter(){
		
			finCounter--;
			theta = theta-0.1f;
			if (finCounter==-10){
				finMovementUp = true;
			}
	
	}
	/**
	 * This method increases the fin counter
	 * 
	 * @see getFrameCounter()
	 */
	private void incSlowFinCounter(){
		
			slowFinCounter++;
			if (slowFinCounter==2){
				slowFinMovementUp = false;
			}
	}
		
	/**
	 * This method increases the fin counter
	 * 
	 * @see getFrameCounter()
	 */
	private void decSlowFinCounter(){
		
			slowFinCounter--;
			if (slowFinCounter==-2){
				slowFinMovementUp = true;
			}
	
	}
}
